package com.wnhz.work.yz.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.base.RecycleViewDivider;
import com.wnhz.work.yz.bean.Royalty;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RoyaltyActivity extends AppCompatActivity implements View.OnClickListener {
    private BaseRVAdapter<Royalty> adapter;
    private List<Royalty> list = new ArrayList<>();
    private RecyclerView recyclerView;
    private static final String TAG = "RoyaltyActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_royalty);
        initTitle();
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        upload();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseRVAdapter<Royalty>(this, list) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_bill;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                View item_bill_lay = holder.getView(R.id.item_bill_lay);
                item_bill_lay.setOnClickListener(RoyaltyActivity.this);
                item_bill_lay.setTag(position);
                holder.setTextView(R.id.week, list.get(position).getWeek());
                holder.setTextView(R.id.day_time, list.get(position).getPosttime());
                holder.setTextView(R.id.money_number, list.get(position).getType() + "¥ " + list.get(position).getAmount());
                holder.setTextView(R.id.type, list.get(position).getWay_name());
                holder.setTextView(R.id.state, "");


            }
        };
        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayout.VERTICAL, 1
                , getResources().getColor(R.color.line_color)));
        recyclerView.setAdapter(adapter);
    }

    private void upload() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.UCENTER_GETTODAYTICHENG, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: zhangdan= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    if (jsonArray != null) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            Royalty bean = new Royalty();

                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonArray.optString(i), Royalty.class);

                            list.add(bean);
                        }
                    }
                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("今日提成");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
